package com.bloveoacom.weichi.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.bloveoacom.base.BaseList2Fragment;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseListSubscriber2;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.ActivityDetailActivity;
import com.bloveoacom.weichi.activity.SearchActivity;
import com.bloveoacom.weichi.bean.ActivitysBean;
import com.bloveoacom.weichi.bean.BaseBean;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.bloveoacom.weight.LlkjToolBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;

import static com.bloveoacom.http.HttpMethods.PAGE_START_SIZE;
import static com.bloveoacom.util.Constant.ACTIVITY_STATUS_JINXINZHONG;
import static com.bloveoacom.util.Constant.ACTIVITY_STATUS_WEIKAISHI;
import static com.bloveoacom.util.Constant.ACTIVITY_STATUS_YIJIESHU;
import static com.bloveoacom.util.Constant.INTENT_ACTIVITYID;
import static com.bloveoacom.util.DataUtil.setItemView;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/25
 * desc  :
 */
public class ActivityFragment extends BaseList2Fragment<ActivitysBean> {

    @BindView(R.id.title_bar)
    LlkjToolBar mToolbar;
    public static ActivityFragment newInstance() {
        Bundle args = new Bundle();
        ActivityFragment fragment = new ActivityFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_activity;
    }

    @Override
    protected View getViewSetTB() {
        return mToolbar;
    }

    @Override
    protected void convertView(BaseViewHolder helper, ActivitysBean item) {
        GlideUtil.loadRoundmage(helper.getView(R.id.iv_image), StringUtils.getFullUrl(item.getPosters(), 0));
        if (item.getIsCanTicket()) {
            helper.setText(R.id.tv_buy, "去购买");
            helper.setBackgroundRes(R.id.tv_buy, R.drawable.shape_buy);
        } else {
            //未购票
            if(item.isCanTicket==1){
                helper.setText(R.id.tv_buy, "已购票");
            }else{
                helper.setText(R.id.tv_buy, "去购买");
            }
            helper.setBackgroundRes(R.id.tv_buy, R.drawable.shape_buy_gray);
        }
        if(item.getActivityStatus()==ACTIVITY_STATUS_WEIKAISHI){
            helper.setText(R.id.tv_state, "未开始");
            helper.setBackgroundRes(R.id.tv_state, R.drawable.shape_buy_gray);
        } if(item.getActivityStatus()==ACTIVITY_STATUS_JINXINZHONG){
            helper.setText(R.id.tv_state, "活动中");
            helper.setBackgroundRes(R.id.tv_state, R.drawable.shape_buy);
        } if(item.getActivityStatus()==ACTIVITY_STATUS_YIJIESHU){
            helper.setText(R.id.tv_state, "已结束");
            helper.setBackgroundRes(R.id.tv_state, R.drawable.shape_buy_gray);
        }
        helper.setText(R.id.tv_title,item.title);
    }

    @Override
    public void initView() {
        super.initView();
        mToolbar.setToolBar("活动",
                false, "", LlkjToolBar.isNull, true, "", R.drawable.ic_search);
    }

    @Override
    protected void getData() {
        BaseListSubscriber2<List<ActivitysBean>> baseSubscriber2 = HttpMethods.getInstance().getHttpService().getActivityList(page, getPageSize(),UserInfoBean.getInstance().getToken(),"").compose(RxHelper.getData()).subscribeWith(new BaseListSubscriber2<List<ActivitysBean>>(this,page==PAGE_START_SIZE,page) {
            @Override
            public void onNext(List<ActivitysBean> o) {
                super.onNext(o);
                doData(o);
            }
        });
        addDispose(baseSubscriber2);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_title;
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString(INTENT_ACTIVITYID,datas.get(position).getActivityId());
                ActivityUtil.startActivity(mContext,ActivityDetailActivity.class,bundle);
            }
        });
        mToolbar.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.startActivity(mContext, SearchActivity.class,"1");
            }
        });
    }
}
